/*
* i18n 国际化 与 vant 移动端组件库国际化
* */
import { useI18n } from 'vue-i18n'
import { Locale } from 'vant'
import enUS from 'vant/es/locale/lang/en-US';
import zhCN from 'vant/es/locale/lang/zh-CN';

const useLocal = defineStore('locale', () => {
    const config = useRuntimeConfig()
    const i18n = useI18n()
    const locale = computed(() => i18n.locale.value)

    const changeLocale = (value: string | null) => {
        if(i18n.locale.value === value || Object.prototype.toString.call(value) === '[object Null]') return
        i18n.locale.value = value as string
        value === 'en' ? Locale.use('en-US', enUS) : Locale.use('zh-CN', zhCN)
        localStorage.setItem(config.public.NUXT_WEB_LOCAL, value as string)
    }

    const localeParams = () => {
        return i18n.locale.value == 'zh-cn' ? 'en' : 'zh-cn'
    }

    return { locale, changeLocale, localeParams }
})

export default useLocal